package org.finesys.common.security.core.jackson;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.finesys.common.security.authentication.password.OAuth2ResourceOwnerPasswordAuthenticationToken;
import org.finesys.common.security.core.module.AuthUser;
import org.springframework.security.jackson2.SecurityJackson2Modules;

import java.util.List;

public class Auth2Jackson2Module extends SimpleModule {

    public Auth2Jackson2Module() {
        super(Auth2Jackson2Module.class.getName(), new Version(1, 0, 0, null, null, null));
    }

    @Override
    public void setupModule(SetupContext context) {
        SecurityJackson2Modules.enableDefaultTyping(context.getOwner());
        List<Module> getModules = SecurityJackson2Modules.getModules(this.getClass().getClassLoader());
        getModules.forEach(module -> {
            module.setupModule(context);
        });
        context.setMixInAnnotations(OAuth2ResourceOwnerPasswordAuthenticationToken.class, OAuth2ResourceOwnerPasswordAuthenticationTokenMixin.class);
        context.setMixInAnnotations(AuthUser.class, AuthUserMixin.class);
    }
}
